/** 
 * Copyright (C), 2010-2012, AIYOU Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:TietiaoServiceImpl.java
 * Version:1.0
 * Date:2011-12-14
 * Description: 
 * author:zhijian.zhang
 */
package com.tietiao.service.tietiao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.tietiao.dao.TietiaoMapper;
import com.tietiao.dao.UserCommidityFavoriteMapper;
import com.tietiao.dao.model.Tietiao;
import com.tietiao.service.commodity.CommodityService;

@Service
public class TietiaoServiceImpl implements TietiaoService {

	@Resource
	private TietiaoMapper tietiaoMapper;

	@Resource
	private CommodityService commodityService;

	@Resource
	private UserCommidityFavoriteMapper userCommidityFavoriteMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.tietiao.TietiaoService#addTieTiao(com.tietiao.dao
	 * .model.Tietiao)
	 */
	@Override
	public void addTieTiao(Tietiao tietiao) {
		tietiaoMapper.insert(tietiao);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.tietiao.TietiaoService#tietiao(com.tietiao.dao.model
	 * .Tietiao)
	 */
	@Override
	public synchronized void tietiao(List<Tietiao> tietiaos) {
		// 查找未贴条的数据
		List<Tietiao> extTietiaos = tietiaoMapper.selectExtTietiaos(tietiaos);
		if (extTietiaos != null && extTietiaos.size() > 0) {
			for (Tietiao tietiao : extTietiaos) {
				for (int i = 0, len = tietiaos.size(); i < len; i++) {
					if (tietiao.getCommodityId() == tietiaos.get(i)
							.getCommodityId()) {
						tietiaos.remove(tietiaos.get(i));
						break;
					}
				}
			}
		}
		if (tietiaos == null || tietiaos.size() == 0) {
			return;
		}
		// 添加贴条数据
		tietiaoMapper.batchInsert(tietiaos);
		// 更新商品表中被贴条数
		String[] commodityIds = new String[tietiaos.size()];

		for (int i = 0, len = tietiaos.size(); i < len; i++) {
			commodityIds[i] = String.valueOf(tietiaos.get(i).getCommodityId());
		}
		commodityService.batchUpdateTietiaoNum(true, commodityIds);
		// 更新收藏夹数量
		userCommidityFavoriteMapper.updateFavCommodityNum(tietiaos.get(0)
				.getFavoriteId(), tietiaos.size());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tietiao.service.tietiao.TietiaoService#shitiao(int, int)
	 */
	@Override

	public synchronized void shitiao(String[] commodityIds, int userId) {
		List<Tietiao> tietiaos = new ArrayList<Tietiao>();
		for (int i = 0; i < commodityIds.length; i++) {
			Tietiao tietiao = new Tietiao();
			tietiao.setCommodityId(Integer.parseInt(commodityIds[i]));
			tietiao.setUserId(userId);
			tietiaos.add(tietiao);
		}
		// 查找已贴条的商品
		List<Tietiao> extTietiaos = tietiaoMapper.selectExtTietiaos(tietiaos);
		if (extTietiaos == null || extTietiaos.size() == 0) {
			return;
		}
		// 删除贴条数据
		tietiaoMapper.batchDeleteByList(extTietiaos);
		// 更新商品表中被贴条数(将商品的贴条数减一)
		String[] extCommodityIds = new String[tietiaos.size()];
		Map<Integer, Integer> favMap = new HashMap<Integer, Integer>();
		for (int i = 0, len = extTietiaos.size(); i < len; i++) {
			int commodityId = extTietiaos.get(i).getCommodityId();
			int favId = extTietiaos.get(i).getFavoriteId();
			extCommodityIds[i] = String.valueOf(commodityId);
			favMap.put(favId,
					favMap.get(favId) == null ? 1 : 1 + favMap.get(favId));
		}
		commodityService.batchUpdateTietiaoNum(false, extCommodityIds);
		// 更新收藏夹数量(效率低下)
		Set<Map.Entry<Integer, Integer>> mapSets = favMap.entrySet();
		for (Map.Entry<Integer, Integer> map : mapSets) {
			userCommidityFavoriteMapper.updateFavCommodityNum(map.getKey(),
					-map.getValue());
		}
	}

}
